define(['moment'], function (moment) {
    return ['$rootScope', '$scope', '$filter', '$stateParams', 'config', 'MMWService', '$timeout',"$calculater",
        function ($rootScope, $scope, $filter, $stateParams, config, $MMWService, $timeout, $calculater) {
            $scope.init = function(){
                $scope.setting_data = {
                    fullfill_qty:5,//滿箱數
                    tray_qty:8,//托盤數
                }
                $scope.packing_detail=[];
                // $scope.packing_detail=[
                //     {   
                //         pcs_no:'leti1',
                //         lot_no:'20200603'
                //     },
                //     {   
                //         pcs_no:'leti2',
                //         lot_no:'20200603'
                //     },
                //     {   
                //         pcs_no:'leti3',
                //         lot_no:'20200603'
                //     },
                //     {   
                //         pcs_no:'leti4',
                //         lot_no:'20200603'
                //     }
                // ];
                $scope.data_counting = {
                    remain_num: $scope.setting_data.fullfill_qty * $scope.setting_data.tray_qty,//剩餘數
                    total_list_num: $scope.packing_detail.length,//已裝箱數
                    total_tray_num: 0//托盤總數
                };
                document.getElementById('barcode_no').focus();
                document.getElementById('barcode_no').select();
                $scope.product_ver='';//產品編號
                $scope.product_name='';//產品名稱
                $scope.item_spec='';//產品規格
            };

            $scope.back = function(){
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        $scope.barcode_no = result.text;
                        input_barcode_no_callback();
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('B0214.showAlert.column3'));
                    },
                    orientation : 'landscape'
                });
            };

            // barcode輸入
            $scope.input_barcode_no = function(event){
                if(event.keyCode == 13){
                    $scope.search();
                }
            };

            //更改預設Tray數
            $scope.input_tray_qty = function(event){
                var target = event.currentTarget;
                angular.element(target).addClass('is-focus');
                $calculater.open(function(num, dialog){
                    angular.element(target).removeClass('is-focus');
                    if(num==='0'){
                        $scope.$root.showAlert( $filter('translate')('SMT20.msg.zero_is_not_accepted'));
                    }else{
                      $scope.setting_data.tray_qty = num;
                        $scope.data_counting.remain_num = $scope.setting_data.tray_qty*$scope.setting_data.fullfill_qty;
                        dialog.hide();  
                    }
                },
                function(dialog){
                    angular.element(target).removeClass('is-focus');
                    dialog.hide();
                },
                {
                    defaultNum: $scope.setting_data.tray_qty,
                    decimal: false
                });
            };

            //更改滿箱數
            $scope.input_fullbox_qty = function(event){
                var target = event.currentTarget;
                angular.element(target).addClass('is-focus');
                $calculater.open(function(num, dialog){
                    angular.element(target).removeClass('is-focus');
                    if(num==='0'){
                        $scope.$root.showAlert( $filter('translate')('SMT20.msg.zero_is_not_accepted'));
                    }else{
                        $scope.setting_data.fullfill_qty = num;
                        $scope.data_counting.remain_num = $scope.setting_data.tray_qty*$scope.setting_data.fullfill_qty;
                        dialog.hide(); 
                    }
                },
                function(dialog){
                    angular.element(target).removeClass('is-focus');
                    dialog.hide();
                },
                {
                    defaultNum: $scope.setting_data.fullfill_qty,
                    decimal: false
                });
            };

            // 查詢序號
            $scope.search = function (){
                var barcode = $scope.barcode_no;
                $scope.barcode_no = '';
                if(barcode != ''){
                    let no_repeat = true;
                    if($scope.packing_detail.length>0){
                        no_repeat = $scope.packing_detail.every(function(item){
                            return item.pcs_no!=barcode;
                        })
                    }
                    if(no_repeat){//產品序號無重複
                            $MMWService.sendToServer({
                            uri:'ServicesSMT.Module_SMT.packing_product_info_get',
                            content : {
                                pcs_no:barcode,
                            },
                            success: function(data){
                                if(data!=''){
                                    if(data.packing_detail.lot_no !=''){//没有綁定生產批
                                        if($scope.packing_detail.length===0){
                                            $scope.std_product_ver = data.product_ver;
                                            $scope.std_product_name = data.product_name;
                                        }else{
                                            if((data.product_name+data.product_ver)!=($scope.std_product_name+$scope.std_product_ver)){
                                                $scope.$root.showAlert($filter('translate')('SMT20.msg.different_product_no'))
                                                return;
                                            }
                                        }
                                        $scope.product_ver = data.product_ver;//產品編號
                                        $scope.product_name = data.product_name;//產品名稱
                                        $scope.item_spec = data.item_spec;//產品規格data
                                        data.packing_detail[0].seq = $scope.packing_detail.length + 1;
                                        $scope.packing_detail.unshift(data.packing_detail[0]); 
                                        // 托盤數、總數、剩餘數計算
                                        $scope.data_counting.total_list_num++;
                                        $scope.data_counting.remain_num--;
                                        if($scope.data_counting.total_list_num % $scope.setting_data.tray_qty===0){
                                            $scope.$root.showAlert( $filter('translate')('SMT20.msg.tray_qty_is_full'));
                                            $scope.data_counting.total_tray_num++;
                                        }
                                        if($scope.data_counting.remain_num===0){
                                            $scope.confirm();
                                        }
                                    }else{
                                        $scope.$root.showAlert( $filter('translate')('SMT20.msg.no_lot_no'));
                                    }
                                }else{
                                    $scope.$root.showAlert( $filter('translate')('common.msg.no_data'));
                                }
                            }
                        });
                    }else{
                        $scope.$root.showAlert( $filter('translate')('SMT20.msg.product_no_repeat'));
                    }
                }
            }

            // 刪除打包項目
            $scope.removeRow = function (item) {
                $scope.packing_detail.splice($scope.packing_detail.indexOf(item), 1);
                $scope.packing_detail.forEach(function(item,index){
                    item.seq=$scope.packing_detail.length-index;
                })
                $scope.data_counting.remain_num++;//剩餘數
                $scope.data_counting.total_list_num--;//已裝箱數
            };

            //存檔並列印
            $scope.save_and_print=function(create_list,info_data){
                if($scope.packing_detail.length > 0 ){
                        $MMWService.sendToServer({
                            uri:'ServicesSMT.Module_SMT.packing_save_process',
                            content : {
                                packing_sn:info_data.box_no,
                                only_print:'false',
                                ip: config.server.ip,
                                port: config.server.port,
                                packing_detail:$scope.packing_detail,
                                create_max_detail:create_list
                            },
                            success: function(data){
                                printTag(data.pcs_detail,0);
                                let double_print = $scope.packing_detail;
                                $scope.packing_detail=[];
                                $scope.product_ver = '';//產品編號清除
                                $scope.product_name = '';//產品名稱清除
                                $scope.item_spec = '';//產品規格data
                                //托盤數、裝箱數復原
                                $scope.data_counting.total_tray_num=0;
                                $scope.data_counting.total_list_num=0;
                                $scope.data_counting.remain_num=$scope.setting_data.fullfill_qty * $scope.setting_data.tray_qty;
                                //詢問是否再次列印標籤？
                                $scope.$root.showConfirm($filter('translate')('SMT20.msg.seal_success',info_data),function(confirmDialog){
                                    $MMWService.sendToServer({
                                        uri:'ServicesSMT.Module_SMT.packing_save_process',
                                        content : {
                                            packing_sn:info_data.box_no,
                                            only_print:'true',
                                            ip: config.server.ip,
                                            port: config.server.port,
                                            packing_detail:double_print,
                                            create_max_detail:create_list
                                        },
                                        success: function(data){
                                            confirmDialog.hide();
                                            printTag(data.pcs_detail,0);
                                        }
                                    });
                                });
                                document.getElementById('barcode_no').focus();
                                document.getElementById('barcode_no').select();
                            }
                        });
                }else{
                    $scope.$root.showAlert( $filter('translate')('SMT20.msg.packing_detail_no_data'));
                }
            }
            // 確定強制送出
            $scope.confirm = function(){
                $MMWService.sendToServer({//呼叫箱數服務
                    uri:'ServicesSMT.Module_SMT.packing_generate_box_process',
                    content : {
                        lot_no:$scope.packing_detail[0].lot_no,
                    },
                    success: function(data){
                        if(data!=''){
                            if($scope.data_counting.remain_num!=0){
                                $scope.$root.showConfirm($filter('translate')('SMT20.msg.this_box_not_full',{total_num:$scope.data_counting.total_list_num, box_no:data.box_no}), function(confirmDialog){   
                                    // printTag(data.box_no,0);
                                    confirmDialog.hide();
                                    let print_data={total_num:$scope.data_counting.total_list_num, box_no:data.box_no}
                                    $scope.save_and_print(data.create_max_detail, print_data);
                                }); 
                            }else{
                               $scope.$root.showConfirm($filter('translate')('SMT20.msg.this_box_has_been_full',{total_num:$scope.setting_data.fullfill_qty * $scope.setting_data.tray_qty, box_no:data.box_no}), function(confirmDialog){
                                    // printTag(data.box_no,0);
                                    confirmDialog.hide();
                                    let print_data={total_num:$scope.data_counting.total_list_num, box_no:data.box_no}
                                    $scope.save_and_print(data.create_max_detail, print_data);
                                }); 
                            }
                        }else{
                            $scope.$root.showAlert( $filter('translate')('SMT20.msg.box_no_has_no_data'))
                        }
                    }
                });  
            }

            function printTag(tags, index){
                if(tags && index < tags.length){
                    if(window.nodeRequire){
                        var exec = window.nodeRequire('child_process').exec;
                        // $rootScope.showToast($filter('translate')('SMT19.msg.prpare_print', {index:index}));
                        exec(tags[index].commandline, function(error, stdout, stderr){
                            if(error){
                                $rootScope.showAlert($filter('translate')('E01.msg.cantprint'));
                            }
                            index = index +1;
                            printTag(tags, index);
                        });
                    }
                }
            };
        }
    ];
});
